﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;
using CSCore;
using CSCore.SoundIn;
using CSCore.SoundOut;
using CSCore.Streams;
using FM.Common;
using FM.Events;
using FM.Services;
using FM.录音.Data;
using FM.录音.Events;
using FM.录音.接口;
using FM.接口;
using Microsoft.Practices.Prism.PubSubEvents;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;


namespace FM.录音.Services
{
    public class 监听服务1 : 启动基类, I监听服务
    {
        private double _left;
        private double _right;
        private double _peakUpdateCounter;
        private IWaveSource _finalSource;
        private WasapiCapture _soundIn;
        private WasapiCapture _soundOut;

        private IFm录音配置 _录音配置;

        public 监听服务1(IUnityContainer 人事部cs, IEventAggregator 新闻部cs, IRegionManager 模块cs)
            : base(人事部cs, 新闻部cs, 模块cs)
        {
            新闻部.GetEvent<退出Event>().Subscribe(退出Action, true);
        }

        private void 退出Action(object obj)
        {
            停止监听();
        }


        public IFm录音配置 录音配置
        {
            get { return _录音配置; }
            set { _录音配置 = value; }
        }

        public bool Is监听中 { get; private set; }
        public void Set监听(bool 实时监听)
        {
            if (实时监听 && !Is监听中)
            {
                启动监听();
            }
            else if (!实时监听 && Is监听中)
            {
                停止监听();
            }
        }

        public void 启动监听()
        {
            if (Is监听中)
            {
                return;
            }

            #region 输入声卡

            var 输入声卡 = 声卡设备.查找输入声卡(录音配置.录音声卡);
            if (输入声卡 == null)
            {
                新闻部.GetEvent<状态栏更新Event>().Publish(new 状态栏Data1(Enum状态栏.提示, ID + "没有输入声卡"));
                return;
            }
            if (录音配置.录音声卡 != 输入声卡.FriendlyName)
            {
                录音配置.录音声卡 = 输入声卡.FriendlyName;
            }

            #endregion

            #region 输出声卡

            var 输出声卡 = 声卡设备.查找输出声卡(录音配置.监听声卡);
            if (输出声卡 == null)
            {
                新闻部.GetEvent<状态栏更新Event>().Publish(new 状态栏Data1(Enum状态栏.提示, ID + "没有输出声卡"));
                return;
            }

            if (录音配置.监听声卡 != 输出声卡.FriendlyName)
            {
                录音配置.监听声卡 = 输出声卡.FriendlyName;
            }

            #endregion

            Is监听中 = true;


            _soundIn = new WasapiCapture { Device = 输入声卡 };
            _soundIn.Initialize();

            var soundInSource = new SoundInSource(_soundIn);
            var singleBlockNotificationStream = new SingleBlockNotificationStream(soundInSource);
            _finalSource = singleBlockNotificationStream.ToWaveSource();

            byte[] buffer = new byte[_finalSource.WaveFormat.BytesPerSecond / 2];
            soundInSource.DataAvailable += (s, e) => { _finalSource.Read(buffer, 0, buffer.Length); };

            //_soundOut = new WasapiCapture { Device = 输出声卡 };
            //_soundOut.Initialize();



            _soundIn.Start();
        }
        public void 停止监听()
        {
            if (!Is监听中)
            {
                return;
            }

            Is监听中 = false;

            if (_soundIn != null)
            {
                _soundIn.Stop();
                _soundIn.Dispose();
            }
            if (_finalSource != null)
                _finalSource.Dispose();

        }



        public int ID { get; set; }
    }
}